﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

using Castle.Core.Logging;

using InfoGenerateSystem.Core.Utils;
using InfoGenerateSystem.Component;
using InfoGenerateSystem.Domain;
using InfoGenerateSystem.Persistence.IDao;
using InfoGenerateSystem.Service.Interfaces;

namespace InfoGenerateSystem.Service.Implements
{
    /// <summary>
    /// 标签配置装载类
    /// 
    /// 创建人:     王昊
    /// 创建时间:   2009-05-04
    /// 
    /// </summary>
    public class ConfigLoader
    {
        private static IGenericDao _dao = ServiceHelper.GetService(typeof(IGenericDao)) as IGenericDao;
        private static ILogger _log = ServiceHelper.GetService(typeof(ILogger)) as ILogger;

        //通过配置文件名读取标签配置,Excel导出和网页截图通用
        public static IList<TblbLabel> LoadLabelsConfig(String configFileName)
        {
            IList<TblbLabel> labelList = new List<TblbLabel>();

            try
            {
                String configString = LoadConfigString(configFileName);

                if (configString.Contains("[Labels]"))
                {
                    String urlString = configString.Substring(configString.IndexOf("[Labels]\n") + 9);
                    urlString = urlString.Remove(urlString.IndexOf("\n[EndLabels]"));

                    String[] configLines = urlString.Split('\n');
                    foreach (String configLine in configLines)
                    {
                        String secid = configLine;
                        TblbLabel lbl = _dao.Find("TblbLabel.Find", Convert.ToInt64(secid)) as TblbLabel;
                        labelList.Add(lbl);
                    }
                }
            }
            catch (IndexOutOfRangeException)
            {
                //throw new Exception("配置文件Config/ExcelDataLabels.ini有格式错误");
            }
            catch (FormatException) { }
            catch (Exception) { }

            return labelList;
        }

        public static ExcelExportConfig LoadExcelExportConfig(String configFileName) 
        {
            ExcelExportConfig eec = new ExcelExportConfig();
            String configString = LoadConfigString(configFileName);

            if (configString.Contains("[Config]"))
            {
                String excelConfig = configString.Substring(configString.IndexOf("[Config]") + 8);
                excelConfig = excelConfig.Remove(excelConfig.IndexOf("[EndConfig]"));
                String[] excelConfigLines = excelConfig.Split('\n');
                foreach (String aLine in excelConfigLines)
                {
                    String[] line = new String[2]; ;
                    if (aLine.Contains(" "))
                    {
                        line = aLine.Split(' ');
                    }
                    if (line[0] == "RunningState")
                    {
                        if (line.Length > 1)
                        {
                            eec.RunningState = line[1];
                        }
                    }
                    if (line[0] == "TimerMinutes")
                    {
                        if (line.Length > 1)
                        {
                            eec.TimerMinutes = Convert.ToInt32(line[1]);
                        }
                    }
                    if (line[0] == "TimerStart")
                    {
                        if (line.Length > 1)
                        {
                            eec.TimerStart = Convert.ToDateTime(line[1]);
                        }
                    }
                }
            }
            return eec;
        }

        public static SnagConfig LoadSnagConfig(String configFileName) 
        {
            SnagConfig sc = new SnagConfig();
            String configString = LoadConfigString(configFileName);

            if (configString.Contains("[Config]"))
            {
                String imageConfig = configString.Substring(configString.IndexOf("[Config]") + 8);
                imageConfig = imageConfig.Remove(imageConfig.IndexOf("[EndConfig]"));
                String[] imageConfigLines = imageConfig.Split('\n');
                foreach (String aLine in imageConfigLines)
                {
                    String[] line = new String[2]; ;
                    if (aLine.Contains(" "))
                    {
                        line = aLine.Split(' ');
                    }
                    if (line[0] == "RunningState")
                    {
                        if (line.Length > 1)
                        {
                            sc.RunningState = line[1];
                        }
                    }
                    if (line[0] == "TimerMinutes")
                    {
                        if (line.Length > 1)
                        {
                            sc.TimerMinutes = Convert.ToInt32(line[1]);
                        }
                    }
                    if (line[0] == "TimerStart")
                    {
                        if (line.Length > 1)
                        {
                            sc.TimerStart = Convert.ToDateTime(line[1]);
                        }
                    }
                    else if (line[0] == "SrcWidth")
                    {
                        if (line.Length > 1)
                        {
                            sc.ScrWidth = Convert.ToInt32(line[1]);
                        }
                    }
                    else if (line[0] == "SrcHeight")
                    {
                        if (line.Length > 1)
                        {
                            sc.ScrHeight = Convert.ToInt32(line[1]);
                        }
                    }
                    else if (line[0] == "ImageWidth")
                    {
                        if (line.Length > 1)
                        {
                            sc.ImageWidth = Convert.ToInt32(line[1]);
                        }
                    }
                    else if (line[0] == "ImageHeight")
                    {
                        if (line.Length > 1)
                        {
                            sc.ImageHeight = Convert.ToInt32(line[1]);
                        }
                    }
                }
            }
            return sc;
        }

        public static IList<ImageWebPage> LoadWebPagesConfigForSnag(String configFileName)
        {
            IList<ImageWebPage> webList = new List<ImageWebPage>();

            try
            {
                String configString = LoadConfigString(configFileName);

                if (configString.Contains("[Url]"))
                {
                    String urlString = configString.Substring(configString.IndexOf("[Url]\n") + 6);
                    urlString = urlString.Remove(urlString.IndexOf("\n[EndUrl]"));

                    String[] configLines = urlString.Split('\n');
                    foreach (String configLine in configLines)
                    {
                        String url = configLine;
                        ImageWebPage webPage = new ImageWebPage();
                        webPage.Url = url;
                        webList.Add(webPage);
                    }
                }
            }
            catch (IndexOutOfRangeException)
            {
                //throw new Exception("配置文件Config/ExcelDataLabels.ini有格式错误");
            }
            catch (FormatException) { }

            return webList;
        }

        public static ExportImageConfig LoadExportImageConfig(String configFileName)
        {
            ExportImageConfig eic = new ExportImageConfig();
            String configString = LoadConfigString(configFileName);

            if (configString.Contains("[Config]"))
            {
                String imageConfig = configString.Substring(configString.IndexOf("[Config]") + 8);
                imageConfig = imageConfig.Remove(imageConfig.IndexOf("[EndConfig]"));
                String[] imageConfigLines = imageConfig.Split('\n');
                foreach (String aLine in imageConfigLines)
                {
                    String[] line = new String[2]; ;
                    if (aLine.Contains(" "))
                    {
                        line = aLine.Split(' ');
                    }
                    if (line[0] == "RunningState")
                    {
                        if (line.Length > 1)
                        {
                            eic.RunningState = line[1];
                        }
                    }
                    if (line[0] == "TimerMinutes")
                    {
                        if (line.Length > 1)
                        {
                            eic.TimerMinutes = Convert.ToInt32(line[1]);
                        }
                    }
                    if (line[0] == "TimerStart")
                    {
                        if (line.Length > 1)
                        {
                            eic.TimerStart = Convert.ToDateTime(line[1]);
                        }
                    }
                    else if (line[0] == "ImagePercent")
                    {
                        if (line.Length > 1)
                        {
                            eic.ImagePercent = Convert.ToInt32(line[1]);
                        }
                    }
                }
            }
            return eic;
        }

        private static String[] LoadConfigLines(String configFileName)
        {
            String configString = LoadConfigString(configFileName);

            String[] configLines = configString.Split('\n');

            return configLines;
        }

        private static String LoadConfigString(String configFileName)
        {
            String configString = FileHelper.ReadFileString(configFileName);

            configString = configString.Trim();
            configString = configString.Replace("\r", String.Empty);
            String[] configLines = configString.Split('\n');

            configString = String.Empty;
            foreach (String configLine in configLines)
            {
                String aLine = configLine.Trim();
                if (((!String.IsNullOrEmpty(aLine))) && (!aLine.StartsWith("#")))
                {
                    if (aLine.Contains("#"))
                    {
                        aLine = aLine.Split('#')[0];
                    }
                    configString += aLine + "\n";
                }
            }
            configString = configString.Remove(configString.Length - 1);

            return configString;
        }
    }
}
